﻿
using Babylon.Toolbox;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Babylon.Toolkit.EffectSystemSample.ParallaxEffect
{
    public class ParallaxEffect : Effect
    {
        public ParallaxEffect(GraphicsDevice device) : base(device, "Babylon.Toolkit.EffectSystemSample", "ParallaxEffect/Parallax")
        {
            World = Matrix.Identity;
            View = Matrix.Identity;
            Projection = Matrix.Identity;
            CameraPosition = Vector3.Zero;
            LightDir = Vector3.Zero;
            DiffuseColor = new Vector4(1);
            AmbientColor = new Vector4(0.2f);
            SpecularColor = new Vector3(1);
            SpecularExponent = 16;
            _worldViewProjectionParam = GetParameter("WorldViewProjection");
            _worldParam = GetParameter("World");
            _cameraPositionParam = GetParameter("CameraPosition");
            _lightDirParam = GetParameter("LightDir");
            _diffuseColorParam = GetParameter("DiffuseColor");
            _ambientColorParam = GetParameter("AmbientColor");
            _specularColorParam = GetParameter("SpecularColor");
            _heightScaleParam = GetParameter("HeightScale");
        }

        public override void Apply()
        {
            _worldViewProjectionParam.SetValue(World*View*Projection);
            _worldParam.SetValue(World);
            _cameraPositionParam.SetValue(CameraPosition);
            _lightDirParam.SetValue(LightDir);
            _diffuseColorParam.SetValue(DiffuseColor);
            _ambientColorParam.SetValue(AmbientColor);
            _specularColorParam.SetValue(new Vector4(SpecularColor, SpecularExponent));
            _heightScaleParam.SetValue(HeightScale);
            Device.Textures[0] = DiffuseTexture;
            Device.Textures[1] = NormalHeightMapTexture;
            base.Apply();
        }

        private EffectParameter _worldViewProjectionParam;
        private EffectParameter _worldParam;
        private EffectParameter _cameraPositionParam;
        private EffectParameter _lightDirParam;

        private EffectParameter _diffuseColorParam;
        private EffectParameter _ambientColorParam;
        private EffectParameter _specularColorParam;
        private EffectParameter _heightScaleParam;

        public Texture2D DiffuseTexture { get; set; }
        public Texture2D NormalHeightMapTexture { get; set; }

        public Matrix World { get; set; }
        public Matrix View { get; set; }
        public Matrix Projection { get; set; }
        public Vector3 CameraPosition { get; set; }
        public Vector3 LightDir { get; set; }

        public Vector4 DiffuseColor { get; set; }
        public Vector4 AmbientColor { get; set; }
        public Vector3 SpecularColor { get; set; }
        public float SpecularExponent { get; set; }

        public float HeightScale { get; set; }

    }
}
